﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WeiXinSDK.MP.Model;

namespace Web.Controllers
{
	/// <summary>
	/// Token服务
	/// </summary>
	public class AccessTokenController : ApiController
	{
		private static AccessTokenModel Token { get; set; }

		// GET: api/AccessToken
		public AccessTokenModel Get()
		{
			if (Token == null || Token.ExpirationTime < DateTime.Now)
			{
				lock ("Web.Controllers.AccessTokenController")
				{
					if (Token == null || Token.ExpirationTime < DateTime.Now)
					{
						Token = new WeiXinSDK.MP.Api().GetAccessToken().Result;
					}
				}
			}
			return Token;
		}

		// Post: api/AccessToken
		public AccessTokenModel Refresh()
		{
			Token = new WeiXinSDK.MP.Api().GetAccessToken().Result;
			return Token;
		}
	}
}
